/**
 * 
 */
$(document).ready(function () {
	/**
	 * Redirige l'utilisateur si il est déjà connectée
	 */
	if(auth != null && auth.connected == false) {
		window.location.href = "/index.html";
	}
	
	$.ajax('vol',{
		type:'GET',
		dataType: "json",
		data: {
			cmd:'listeSearch',
        },
	}).done(function(data, testStatus, jqXHR){
		for(var search in data)
		{
			$("#searchList").append(
			"<tr><td>" +
			data[search].date + 
			"</td><td>" +
			data[search].leavingFrom +
			"</td><td>" +
			data[search].goingTo +
			"</td><td>" +
			data[search].departure +
			"</td><td>" +
			data[search].responseCount +
			"</td><td>" +
			"<a class=\"btn btn-primary btn-block seeHistoric\" href=\"\"id=\"" +
			data[search].id +
			"\" >Voir</a>" +
			"</td></tr>"		
			);
		};
	}).fail(function(jqXHR, textStatus, errorThrown) {
        	setAlertMessage('Erreur!',errorThrown+ ' (' + textStatus +')','danger');
	});
	
	$("#searchList").delegate(".seeHistoric", "click", function() {
		
		$.ajax('vol',{
            type: "PUT",
            dataType: 'json',
            data: {
            	cmd : "updateSearch",
            	id : $(this).attr("id"),
            },
        }).done(function(data, textStatus, jqXHR) {
        	if (data.redirect) {
        		window.location.href = data.redirect;
        	}
        }).fail(function(jqXHR, textStatus, errorThrown) {
        	setAlertMessage('Erreur!',errorThrown+ ' (' + textStatus +')','danger');
        });
		return false;
	});
});